var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		initGradeClass();
	});
});
function initGradeClass(){
	var cont = "";
	var classcont = $(".classes-choice",parent.document);
	if(classcont.length > 0){
		classcont.each(function(){
			cont += "<option value='"+$(this).attr("classcode")+"' type='"+$(this).attr("type")+"'>"+$(this).text()+"</option>";
		});
	}else{
		cont += "<option value='' type=''>班级</option>";
	}
	$("#exam-classcode").html(cont);
	
	var type = $("#exam-classcode option:selected").attr("type");
	getAllCourseSubject(type);
	
	$("#exam-classcode").unbind("change");
	$("#exam-classcode").change(function(){
		var type = $("#exam-classcode option:selected").attr("type");
		getAllCourseSubject(type);
	});
}

function getAllCourseSubject(type){
	var userKey = $("#user-name",parent.parent.document).attr("key");	
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var data = {
			cmd:"getAllCourseSubject",
			model:"course",
			userKey:userKey,
			data:{
				type:type,
				schoolcode:schoolcode
			},
			remark:"",
			
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1){
			var cont = "";
			var data = res.data;
			if(data.length > 0){
				for(var i = 0; i < data.length; i++){
					cont += "<option value='"+data[i].subjectcode+"' coursename='"+data[i].coursecontent+"'>"+data[i].coursecontent+"</option>";
				}
			}else{
				cont += "<option value='' coursename=''>科目</option>";
			}
			$("#exam-coursename").html(cont);
		}
	},null,null);
}
function pageBtns(){
	check("exam-examname",isNull,"null");
	check("exam-describes",isNull,"null");
	check("exam-stime",isNull,"null");
	check("exam-etime",isNull,"null");
	$("#save-btns").click(function(){
		if(checkSave()){
			if(check2()){
				addExam();
			}
		}
	});
}

function addExam(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var param = {};
	param.usercode = usercode;
	param.usertype = usertype;
	param.schoolcode = schoolcode;
	param.examname = encodeURI($("#exam-examname").val());
	param.classcode = $("#exam-classcode option:selected").val();
	param.describes = encodeURI($("#exam-describes").val());
	param.subjectcode = $("#exam-coursename option:selected").val();
	param.coursename = encodeURI($("#exam-coursename option:selected").text());
	param.stime = strToDate($("#exam-stime").val() + ":00")/1000;
	param.etime = strToDate($("#exam-etime").val() + ":00")/1000;
	param.createtime = new Date().getTime()/1000;
	
	var data = {
			cmd:"insertTeacherSmallEaxmInfo",
			model:"bigexam",
			userKey:userKey,
			data:param,
			remark:"",						
	};
	processBegin("正在保存中···");
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("添加成功");
		}else{
			processError("添加失败");
		}
	},null,null);
}

function check2(){
	var flg = false;
	var checkFails = $(".check-fail");
	for(var i = 0; i < checkFails.length; i++){
		if(checkFails[i].innerHTML != ""){
			flg = false;
			break;
		}else{
			flg = true;
		}
	}
	return flg;
}

function checkSave(){
	var flag = false;
	var parame = ["examname","classcode","coursename","stime","etime","describes"];
	for(key in parame){
		if($("#exam-" + parame[key]).val() == ""){
			$("#exam-" + parame[key]).focus();
			$("#exam-" + parame[key] + "-ex").text("不能为空！");
			return false;
		}else{
			flag = true;
		};
	}
	return flag;
}

function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}